const Router=require('koa-router')
let router=new Router()
const http=require('https')
const uuid=require('uuid')
let request=require('../libs/request')
const fs=require('fs')
const Path=require('path')
const {root}=require('../libs/config')

let loginRouter=require('./login')
let updateRouter=require('./update')


router.get('/api/getSwiper',async ctx=>{
  let swiper =await ctx.db.query("select * from swiper order by sort")
  ctx.body = {
    err:0,
    data:swiper
  }
})

router.get('/api/getHotMovie',async ctx=>{
  let hotMovie=await ctx.db.query("select * from hot_movie")
  ctx.body={
    err:0,
    data:hotMovie
  }
})

router.get('/api/getHotTrailer',async ctx=>{
  let hotTrailer=await ctx.db.query("select * from hot_trailer")
  ctx.body={
    err:0,
    data:hotTrailer
  }
})

router.get('/api/getLike',async ctx=>{
  let allMovie=await ctx.db.query("select * from all_movie")
  let arr=[]
  for(let i=0;i<=4;i++){
    let arr2 = allMovie.splice(parseInt(Math.random()*allMovie.length),1)
    arr.push(arr2[0])
  }
  ctx.body={
    err:0,
    data:arr
  }
})

router.get('/api/getSearch',async ctx=>{
  let movieNum=ctx.query.movieNum
  let key=ctx.query.key
  ctx.query.page=ctx.query.page?ctx.query.page:1
  let pageIndex=(parseInt(ctx.query.page)-1)*movieNum
  console.log(pageIndex)
  let allMovie=await ctx.db.query(`SELECT * FROM all_movie WHERE title LIKE '%${key}%' LIMIT ?,${movieNum}`,[pageIndex])
  let allPage = Math.ceil(allMovie.length/movieNum)
  ctx.body={
    err:0,
    data:allMovie,
    allPage
  }
})

router.get('/api/getMovieInfo/:id',async ctx=>{
  let id=ctx.params.id
  let movie=await ctx.db.query("select * from all_movie where id=?",[id])
  ctx.body={
    err:0,
    data:movie
  }
})

router.use(loginRouter.routes())
router.use('/api',updateRouter.routes())



module.exports=router